package com.example.mybaties.testone.controller;

import com.example.mybaties.testone.domain.JKManagerData;
import com.example.mybaties.testone.domain.ResponseBean;
import com.example.mybaties.testone.util.FtpUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

@Controller
@RequestMapping(value = "/shear_wave")
public class ShearWaveController {

    private Logger logger = LogManager.getLogger(ShearWaveController.class);

    @RequestMapping(value = "/getShearWave.action")
    public String getShearWaveJsp(){

        return "shearWave";
    }


    @RequestMapping(value = "/getData.action")
    //@RequiresPermissions(value = {"user:add"})
    public @ResponseBody
    JKManagerData getData(String type) throws Exception{
        if(type == null){
            type = "1";
        }
        StringBuilder sb = new StringBuilder();
        UrlResource urlResource = null;
        switch (type){
            case "1": //剪切波
                urlResource = new UrlResource("http://www.whctco.com/download/JQBCODE.txt");
                break;
            case "2"://面波
                urlResource = new UrlResource("http://www.whctco.com/download/MBCODE.txt");
                break;
            case "3"://平台
                urlResource = new UrlResource("http://www.whctco.com/download/JZCODE.txt");
                break;
            case "4"://黑名单
                urlResource = new UrlResource("http://www.whctco.com/download/STBLACK.txt");
                break;
            case "5"://静载分析软件控制
                urlResource = new UrlResource("http://www.whctco.com/download/JZFXCODE.txt");
                break;
        }
        InputStream in = urlResource.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(in, "GBK"));
        String str = null;
        while((str = br.readLine()) != null){
            sb.append(str);
            sb.append("\n");
        }
        logger.debug(sb);
        JKManagerData jkManagerData = new JKManagerData();
        jkManagerData.setValue(sb.toString());
        return jkManagerData;
    }

    @RequestMapping("/upload.action")
    public @ResponseBody ResponseBean upload(String type, String content) throws Exception {
        if(type == null || "".equals(type)){
            return new ResponseBean(1, "失败，没有类型");
        }
        String fileName = null;
        switch (type){
            case "1": //剪切波
                fileName = "JQBCODE.txt";
                break;
            case "2"://面波
                fileName = "MBCODE.txt";
                break;
            case "3"://平台
                fileName = "JZCODE.txt";
                break;
            case "4"://黑名单
                fileName = "STBLACK.txt";
                break;
            case "5"://静载分析软件控制
                fileName = "JZFXCODE.txt";
                break;
        }
        //将\n转换成\r\n
        content = convertNToRN(content);
        logger.debug("content = " + content);

        //获取用户名和密码
        Properties properties = new Properties();
        properties.load(this.getClass().getClassLoader().getResourceAsStream("ftp.properties"));
        String userName = properties.getProperty("userName");
        String password = properties.getProperty("password");

        //开始上传
        InputStream in = FtpUtil.byte2Input(content.getBytes());
        boolean result = FtpUtil.uploadFile("whctco.com", 21, userName, password
        ,"", "/download", fileName, in);
        if(! result){
            return new ResponseBean(1, "失败，上传失败");
        }
        return new ResponseBean(0, "成功");
    }

    /**
     * 将\n转换成\r\n
     */
    private String convertNToRN(String content){
        StringBuilder sb = new StringBuilder();
        String[] arr = content.split("\n");
        for(int i = 0; i < arr.length; i++){
            if(arr[i].equals("")){
                continue;
            }
            sb.append(arr[i]);
            sb.append("\r\n");
        }
        sb.append("\r\n");
        return sb.toString();
    }

}
